package models

import "time"

type Order struct {
	ID int64 `gorm:"primary_key" json:"id"`
	OrderType int `json:"orderType" gorm:"column:order_type"`
	BSUId int `json:"bsuId" gorm:"column:bsu_id;default:null"`
	BCUId int `json:"bcuId" gorm:"column:bcu_id;default:null"`
	BMUId int `json:"bmuId" gorm:"column:bmu_id"`
	Status int `json:"status"`
	Electricity float32 `json:"electricity"`
	StartTime time.Time `json:"startTime" gorm:"column:start_time;default:null"`
	EndTime time.Time `json:"endTime" gorm:"column:end_time;default:null"`
}

func ExistOrderById(id int) bool {
	var order Order
	db.Select("id").Where("id = ?", id).First(&order)
	return order.ID > 0
}

func GetOrdersByBSUId(bsuId int) (orders []Order) {
	db.Where("bsu_id = ?", bsuId).Find(&orders)
	return
}

func GetOrdersByBCUId(bcuId int) (orders []Order) {
	db.Where("bcu_id = ?", bcuId).Find(&orders)
	return
}

func GetOrdersByBMUId(bmuId int) (orders []Order) {
	db.Where("bmu_id = ?", bmuId).Find(&orders)
	return
}

func AddOrderInBMU(bmuId, status int, electricity float32) bool {
	db.Create(&Order{
		BMUId: bmuId,
		Status: status,
		Electricity: electricity,
	})
	return true
}

func AddOrderInBCU(bmuId, bcuId, status int, electricity float32) bool {
	db.Create(&Order{
		BCUId: bcuId,
		BMUId: bmuId,
		Status: status,
		Electricity: electricity,
	})
	return true
}

func AddOrderInBSU(bmuId, bcuId, bsuId, status int, electricity float32) bool {
	db.Create(&Order{
		BSUId: bsuId,
		BCUId: bcuId,
		BMUId: bmuId,
		Status: status,
		Electricity: electricity,
	})
	return true
}

func UpdateOrder(id int, data interface{}) bool {
	db.Model(&Order{}).Where("id = ?", id).Updates(data)
	return true
}

func DeleteOrder(id int) bool {
	db.Where("id = ?", id).Delete(&Order{})
	return true
}
